//日期格式化
export function formatTime(date, format) {
  try {
    let newDate = new Date(date);
    let dic = {
      "Y+": newDate.getFullYear(),
      "M+": newDate.getMonth() + 1,
      "D+": newDate.getDate(), //日
      "h+": newDate.getHours(), //小时
      "m+": newDate.getMinutes(), //分
      "s+": newDate.getSeconds(), //秒
      "q+": Math.floor((newDate.getMonth() + 3) / 3), //季度
      "w+": newDate.getDay(), //周几
      S: newDate.getMilliseconds(), //毫秒
    };
    if (/(Y+)/.test(format)) {
      format = format.replace(
        RegExp.$1,
        (newDate.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }
    for (let k in dic) {
      if (new RegExp(`(${k})`).test(format)) {
        let str = dic[k].toString();
        format = format.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? str : padLeftZero(str)
        );
      }
    }
    return format;
  } catch (error) {
    throw new Error("formatTime失败," + error.message);
  }
}

//添加零
export function padLeftZero(str) {
  try {
    return ("00" + str).substr(str.length);
  } catch (error) {
    throw new Error("padLeftZero失败," + error.message);
  }
}

//多少天，多少年，多少月以前或以后的时间
export function dateAdd(date, dateType, duration) {
  var newDate = new Date(date);
  duration = Math.floor(duration);
  switch (dateType) {
    case "Y":
      newDate.setFullYear(newDate.getFullYear() + duration);
      break;
    case "M":
      // let currentYear = newDate.getFullYear() //当前时间的年
      // let currentMonth = newDate.getMonth() + 1 //当前时间的月
      var currentDay = newDate.getDate(); //当前时间的天

      var currentMonthFirstDay = new Date(newDate.setDate(1)); //当前月的第一天
      var nextMonthFirstDay = new Date(
        currentMonthFirstDay.setMonth(
          currentMonthFirstDay.getMonth() + duration
        )
      );

      var nextYear = nextMonthFirstDay.getFullYear();
      var nextMonth = nextMonthFirstDay.getMonth() + 1;
      var nextDay = currentDay;
      var nexMonthDays = new Date(nextYear, nextMonth, 0).getDate();
      if (nextDay <= nexMonthDays) {
        nexMonthDays = nextDay;
      }
      newDate = new Date(nextYear + "/" + nextMonth + "/" + nexMonthDays);
      break;
    case "D":
      newDate.setDate(newDate.getDate() + duration);
      break;
    case "h":
      newDate = new Date(newDate.getTime() + duration * 60 * 60 * 1000);
      break;
    case "m":
      newDate = new Date(newDate.getTime() + duration * 60 * 1000);
      break;
    case "s":
      newDate = new Date(newDate.getTime() + duration * 1000);
      break;
    default:
      throw new Error("dateAdd：非法的时间类型");
  }
  return newDate;
}
//两个时间进行比较
export function dateDiffS(date1, date2) { 
  let diff = date1.getTime() - date2.getTime();
  return diff
}
//两个时间进行比较
export function dateDiff(date1, date2, type) {
  try {
    let diff = date1.getTime() - date2.getTime();
    switch (type) {
      case "Y":
        diff = date1.getFullYear() - date2.getFullYear();
        break;
      case "M":
        diff =
          date1.getFullYear() * 12 +
          date1.getMonth() -
          (date2.getFullYear() * 12 + date2.getMonth());
        break;

      case "D":
        //把时间截掉
        var tempDate1 = new Date(
          date1.getFullYear(),
          date1.getMonth(),
          date1.getDate()
        );
        var tempDate2 = new Date(
          date2.getFullYear(),
          date2.getMonth(),
          date2.getDate()
        );
        diff = (tempDate1.getTime() - tempDate2.getTime()) / 24;
        break;
      case "h":
        diff /= 60;
        break;

      case "m":
        diff /= 60;
        break;

      case "s":
        diff /= 1000;
        break;

      default:
      //毫秒
    }
    return diff;
  } catch (error) {
    throw new Error("dateDiff失败," + error.message);
  }
}

// 判断日期是否是明天
export function isTomorrow(theDate) {
  var date = new Date(); //当前时间
  var today = new Date(
    date.getFullYear(),
    date.getMonth(),
    date.getDate()
  ).getTime(); //今天凌晨
  var tomorrow = new Date(today + 24 * 3600 * 1000).getTime();
  var afterTomorrow = new Date(tomorrow + 24 * 3600 * 1000).getTime();
  return theDate.getTime() < afterTomorrow && tomorrow <= theDate.getTime();
}

// 判断日期是否是今天
export function isToday(theDate) {
  var date = new Date(); //当前时间
  var today = new Date(
    date.getFullYear(),
    date.getMonth(),
    date.getDate()
  ).getTime(); //今天凌晨
  var tomorrow = new Date(today + 24 * 3600 * 1000).getTime();
  return theDate.getTime() < tomorrow && today <= theDate.getTime();
}

// 判断日期是否是昨天
export function isYestday(theDate) {
  var date = new Date(); //当前时间
  var today = new Date(
    date.getFullYear(),
    date.getMonth(),
    date.getDate()
  ).getTime(); //今天凌晨
  var yestday = new Date(today - 24 * 3600 * 1000).getTime();
  return theDate.getTime() < today && yestday <= theDate.getTime();
}

// 转换YYYYMMDD类型的日期字符串为YYYY-MM-DD类型字符串
export function convertDateStr(dateStr) {
  var pattern = /(\d{4})(\d{2})(\d{2})/;
  return dateStr.replace(pattern, "$1-$2-$3");
}
export function calculateGestationalWeeks(expectChildBirth, currentDate) {
  let pregnancyBeginDate = new Date(expectChildBirth.getTime() - 1000 * 60 * 60 * 24 * 280);
  var dateInterval = (currentDate.getTime() - pregnancyBeginDate.getTime()) / (1000 * 60 * 60 * 24);
  dateInterval = parseInt(dateInterval.toString());
  let index = parseInt((dateInterval / 7).toString());
  if (index >= 0 && index < 40) {
    let day = parseInt((dateInterval % 7).toString());
    if (index == 0) {
      if (day <= 0) {
        return "孕前";
      }
      else {
        return `孕${day}天`;
      }
    }
    else {
      if (day == 0) {
        return `孕${index}周`;
      }
      else {
        return `孕${index}周${day}天`;
      }
    }
  }
  else if (index < 0) {
    return "孕前";
  }
  else {
    return "产后";
  }
}

export function calculateGestationalWeekNum(expectChildBirth, currentDate) {
  let pregnancyBeginDate = new Date(expectChildBirth.getTime() - 1000 * 60 * 60 * 24 * 280);
  var dateInterval = (currentDate.getTime() - pregnancyBeginDate.getTime()) / (1000 * 60 * 60 * 24);
  dateInterval = parseInt(dateInterval.toString());
  return parseInt((dateInterval / 7).toString());
}
